package starruby;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Font;
public class Texture {
  private int[] _data;
  public boolean frozen = false;
  private int _width, _height;
  public Graphics g;
  public Image image;
  
  public static Texture load(String path){
    Image i = null;
    try{
      i = Image.createImage("/images/" + path + ".png");
    }catch(Exception e){
      System.out.println("Texture Load Error:" + "/images/" + path + ".png" + " wasn't found");
    }
    return new Texture(i);
  }
  public Texture(Graphics g, int width, int height){
    _width = width;
    _height = height;
    this.image = null;
    frozen = true;
    this.g = g;
    this.g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,
                           Font.STYLE_PLAIN,
                           Font.SIZE_SMALL));
  }
  public Texture(Image image){
    this.image = image;
    _width = image.getWidth();
    _height = image.getHeight();
    frozen = true;
    //_g = this.image.getGraphics();
  }
  public Texture(int width, int height, boolean transparent){
    _width = width;
    _height = height;
    if(transparent){
      frozen = false;
      _data = new int[width * height]; 
      image = null;
    }else{
      frozen = true;
      image = Image.createImage(width, height);
      g = image.getGraphics();
    }
  }
  private void freeze(){
    frozen = true;
    this.image = Image.createRGBImage(_data, _width, _height, true);
    _data = null;
  }
  
  public int getWidth(){
    return _width;
  }
  
  public int getHeight(){
    return _height;
  }
  
  public void changeHue(int r, int g, int b){
    
    if((r ==0) && (g == 0) &&(b == 0)){ return; }
    
    this.g.setClip(0, 0, this.getWidth(), this.getHeight());
    if(_data == null){
      _data  = new int[this.getWidth() * this.getHeight()]; 
    }
    int alpha = Math.abs(r + g + b) / 3;
    
    r = (r + 255) / 2;
    g = (g + 255) / 2;
    b = (b + 255) / 2;
    
    int color = (alpha << 24) + (r << 16) + (g << 8) + b;
    for(int i = 0; i < _data.length; i++){
      _data[i] = color;
    }
    
    this.g.drawRGB(_data, 0, this.getWidth(), 0, 0, this.getWidth(), this.getHeight(), true);
  }
   
  public void renderTexture(Texture texture, int x, int y, int srcX, int srcY, int width, int height){
    if(!texture.frozen){texture.freeze();}
    if(g != null){
      g.setClip(x, y, width, height);
      try{
      g.drawImage(texture.image, x - srcX, y - srcY, Graphics.LEFT | Graphics.TOP);
      }catch(Exception e){
      }
    }else{
      int[] arr = new int[width * height];
      try{
        texture.image.getRGB(arr, 0, width, srcX, srcY, width, height);
        
        for(int i = 0; i < width; i++){
          for(int j = 0; j < height; j++){
            int t =i + j * width;
            _data[(x + i) + (y + j) * _width] = arr[t];
          }
        }
      }catch(Exception e){
      }
    }
  }
  
  public void renderTexture(Texture texture, int x, int y){
    renderTexture(texture, x, y, 0, 0, texture.getWidth(), texture.getHeight());
  }
  
  public void renderText(String text, int x, int y){
      g.setClip(0, 0, 240, 240);
      g.setColor(0,0,0);
      g.drawString(text, x, y, Graphics.LEFT|Graphics.TOP);
  }
  
  public void fill(int red, int green, int blue){
    g.setClip(0, 0, 240, 450);
    g.setColor(red, green, blue);
    g.fillRect(0, 0, 240, 450);
  }
}
